require("../util.js");
require("../component/components.js");
require("../component/seeHeader.js");
require("../component/basicSlide.js");
require("../component/selfmediaflow.js");
require("../component/footer.js");
require("../../jsMt/util.js");
var userInfo=JSON.parse(sessionStorage.getItem("userInfo"));
if(!userInfo){
    location.href="/loginIndex"
}
console.log(userInfo);
var vm = new Vue({
    el:"#cityAd",
    data:{
        id:"",
        indexs:1,
        loadFlag:false,
        cityAdInfo:{},//主页信息
        pageNumber:"1",
        latitude:"",//纬度
        longitude:"",//经度
        cityAdvList:{},
        cityAdvList1:[],
        cityAdvList2:[],//人气列表
        cityNearbyAdvList:{},
        cityNearbyAdvList1:[],
        cityNearbyAdvList2:[]//附近列表
    },
    mounted:function(){
        //sessionStorage.clear()
        this.getMain();
        this.getCityAdvList();
        lazyload();
    },
    methods:{
        bgimg:function(url){
            url = url?url:"";
            return {
                "background-image":'url("'+url+'"),url("")'
            }
        },
        //城市广告主页信息
        getMain:function(){
            var that=this;
            this.ajax({url:"/cityAd/getMain",dataType:"json",method:"post"},function(b){
                if(b.json.code==0&&b.json.data){
                    that.cityAdInfo=b.json.data;
                }else{
                    vm.$refs.prompt.toast("获取主页信息失败！")
                }
            });
        },
        //人气切换
        centTitleTab:function(index){
            this.indexs=index;
            this.pageNumber=1;
            if(this.indexs==1){
                this.getCityAdvList();
            }else if(this.indexs==2){
                this.getCityNearbyAdvList();
            }
        },
        //获取自媒体分类人气分页信息
        getCityAdvList:function(){
            var that=this;
            var data={
                pageNumber:that.pageNumber
            };
            this.ajax({url:"/cityAd/getCityAdvList",data:data,dataType:"json",method:"post"},function(b){
                if(b.json.code==0&&b.json.data){
                    that.cityAdvList=b.json.data;
                    that.cityAdvList1=that.cityAdvList.list;
                    if(that.cityAdvList.pageNumber>1){
                        if(that.cityAdvList.list.length>0) {
                            that.cityAdvList2 = that.cityAdvList2.concat(b.json.data.list);
                        }
                    }else{
                        that.cityAdvList2= that.cityAdvList1;
                    }
                    vm.loadFlag=false;
                    lazyload();
                }else{
                    vm.$refs.prompt.toast("获取人气列表失败！")
                }
                console.log(that.cityAdvList2)
            });
        },
        /*//获取自媒体分类页附近分页信息
        getCityNearbyAdvList:function(){
            var that=this;
            var data={
                pageNumber:that.pageNumber,
                latitude:that.latitude,
                longitude:that.longitude
                // latitude:"29.92",
                // longitude:"116.63"
            };
            console.log(data.latitude+"dddddddddddd"+data.longitude)
            this.ajax({url:"/cityAd/getCityNearbyAdvList",data:data,dataType:"json",method:"post"},function(b){
                if(b.json.code==0&&b.json.data){
                    that.cityNearbyAdvList=b.json.data;
                    that.cityNearbyAdvList1=that.cityNearbyAdvList.list;
                    if(that.cityNearbyAdvList.pageNumber>1){
                        if(that.cityNearbyAdvList.list.length>0) {
                            that.cityNearbyAdvList2 = that.cityNearbyAdvList2.concat(b.json.data.list);
                        }
                    }else{
                        that.cityNearbyAdvList2= that.cityNearbyAdvList1;
                    }
                    vm.loadFlag=false;
                    console.log(that.nearbyAdvList)
                }else{
                    vm.loadFlag=false;
                    vm.$refs.prompt.toast("获取附近列表失败！");
                }
            });
        },*/
        getVideo:function(){
            vm.$refs.prompt.toast("请前往APP端发布视频！");
        },
        getNext:function(item){
            location.href="/see/cityAdInfo?cityId="+item.id+"&cityName="+encodeURI(item.cityName);
        }
    }
    ,watch:{
        cityAdvList2:function(){
            console.log(this.pageNumber)
                this.$nextTick(function(){
                    console.log($(".selfMedia-tabCent").height());
                    fmasonry();
                    $("img.lazy").lazyload({
                        failurelimit:10,
                        load:fmasonry
                    });
                    console.log($(".selfMedia-tabCent").height());
                });
        }
        /*cityNearbyAdvList2:function(){
            this.$nextTick(function(){
                console.log($(".selfMedia-tabCent").height());
                fmasonry();
                $("img.lazy").lazyload({
                    failurelimit:10,
                    load:fmasonry
                });
            });
        }*/
    }
    // ,updated:function(){
    //     fmasonry();
    // }
});
//文档高度
function getDocumentTop() {
    var scrollTop = 0, bodyScrollTop = 0, documentScrollTop = 0;
    if (document.body) {
        bodyScrollTop = document.body.scrollTop;
    }
    if (document.documentElement) {
        documentScrollTop = document.documentElement.scrollTop;
    }
    scrollTop = (bodyScrollTop - documentScrollTop > 0) ? bodyScrollTop : documentScrollTop;    return scrollTop;
}
//可视窗口高度
function getWindowHeight() {
    var windowHeight = 0;
    if (document.compatMode == "CSS1Compat") {
        windowHeight = document.documentElement.clientHeight;
    } else {
        windowHeight = document.body.clientHeight;
    }
    return windowHeight;
}
//滚动条滚动高度
function getScrollHeight() {
    var scrollHeight = 0, bodyScrollHeight = 0, documentScrollHeight = 0;
    if (document.body) {
        bodyScrollHeight = document.body.scrollHeight;
    }
    if (document.documentElement) {
        documentScrollHeight = document.documentElement.scrollHeight;
    }
    scrollHeight = (bodyScrollHeight - documentScrollHeight > 0) ? bodyScrollHeight : documentScrollHeight;
    return scrollHeight;

}
window.onscroll=function(){
    var loadFlag;
    if(vm.indexs==1){
        loadFlag = vm.cityAdvList.lastPage;
    }else if(vm.indexs==2){
        loadFlag = vm.cityNearbyAdvList.lastPage;
    }
    //监听事件内容
    if(getScrollHeight()==getWindowHeight() + getDocumentTop()){
        //当滚动条到底时,这里是触发内容
        if(!loadFlag){
            vm.loadFlag=true;
            vm.pageNumber++;
            if(vm.indexs==1){
                vm.getCityAdvList();
            }else if(vm.indexs==2){
                vm.getCityNearbyAdvList();
            }
        }else{
            vm.$refs.prompt.toast("没有更多了！")
        }
    }
};
function fmasonry() {
    minigrid('.selfMedia-tabCent', '.selfMedia-list',9);
    // $(".selfMedia-tabCent").masonry({
    //     gutterWidth: 10,
    //     itemSelector: '.selfMedia-list',
    //     isAnimated: true,
    //     isFitWidth:true
    // });
}
function lazyload(){
    $("img.lazy").lazyload();
}